<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="app-circle" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useDict } from "/$/dict";

const { dict } = useDict();

const { service } = useCool();

// cl-upsert 配置
const Upsert = useUpsert({
	// 编辑
	async onInfo(data, { done }) {
		if (typeof data.categoryIds === "string") {
			data.categoryIds = data.categoryIds.split(",").map((x: any) => Number(x));
		}
		done(data);
	},
	// 提交
	async onSubmit(data, { done, next }) {
		data.categoryIds = data.categoryIds.join(",");
		done();
		next(data);
	},
	items: [
		{
			prop: "cover",
			label: "圈子封面",
			required: true,
			component: {
				name: "cl-upload",
				props: {
					multiple: true
				}
			}
		},
		{
			prop: "name",
			label: "圈子名称",
			required: true,
			span: 12,
			component: {
				name: "el-input"
			}
		},
		{
			prop: "categoryIds",
			label: "圈子分类",
			required: true,
			span: 12,
			value: [],
			component: {
				name: "el-select",
				props: {
					multiple: true
				},
				options: dict.get("category")
			}
		},
		{
			prop: "userName",
			label: "创 建 者",
			required: true,
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "圈子状态",
			prop: "status",
			span: 12,
			value: 1,
			component: {
				name: "el-select",
				options: [
					{ label: "正常", value: 1 },
					{ label: "禁用", value: 0 }
				]
			},
			required: true
		},
		{
			prop: "description",
			label: "圈子描述",
			required: true,
			component: {
				name: "el-input",
				props: {
					type: "textarea",
					rows: 4
				}
			}
		}
	]
});

// cl-table 配置
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ prop: "id", label: "ID", width: 60 },
		{ prop: "cover", label: "封面", component: { name: "cl-image", props: { size: 60 } } },
		{
			prop: "categoryIds",
			label: "分类",
			formatter(row) {
				if (typeof row.categoryIds === "string") {
					return row.categoryIds.split(",").map((x: any) => Number(x));
				}
				return row.categoryIds;
			},
			dict: dict.get("category")
		},
		{ prop: "name", label: "名称" },
		{ prop: "followCount", label: "关注" },
		{ prop: "postCount", label: "帖子" },
		{ prop: "userName", label: "创建者" },
		{
			label: "状态",
			prop: "status",
			dict: [
				{ label: "禁用", value: 0, type: "danger" },
				{ label: "正常", value: 1, type: "success" }
			]
		},
		{ prop: "createTime", label: "创建时间", sortable: "desc", width: 160 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.app.circle
	},
	(app) => {
		app.refresh();
	}
);
</script>
